package com.company.bookstore.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.company.bookstore.entities.order_module.Logistics;
import com.company.bookstore.entities.order_module.LogisticsType;
import com.company.bookstore.entities.order_module.Order;
import com.company.bookstore.mapper.order_module.LogisticsMapper;
import com.company.bookstore.mapper.order_module.LogisticsTypeMapper;
import com.company.bookstore.mapper.order_module.OrderMapper;
import com.company.bookstore.qo.order_module.OrderQo;
import com.company.bookstore.service.order_module.LogisticsService;
import com.company.bookstore.service.order_module.OrderService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;


/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 赵云霞
 * @since 2020-09-08
 */
@Service
public class LogisticsServiceImpl extends ServiceImpl<LogisticsMapper, Logistics> implements LogisticsService {

    @Resource
    LogisticsMapper logisticsMapper;
    @Override
    public Logistics getLogistics(int oid) {
        Logistics logistics = logisticsMapper.getLogis(oid);
        return logistics;
    }

    @Override
    public Logistics createLogistics(int oid) {

        Logistics logistics = new Logistics();
        //判断oid是否存在，存在则更新，不存在则新增
        int faces  = 6;
        Random r = new Random();
        logistics = getById(oid);

        if(logistics!=null){
            QueryWrapper<Logistics> queryWrapper = new QueryWrapper<Logistics>().eq("fk_order_id",oid);
            update(logistics,queryWrapper);
        }
        else{
            logistics = Logistics.builder()
                    .logNum(UUID.randomUUID().toString())
                    .deleted(0)
                    .fkOrderId(oid)
                    .fkTypeId(r.nextInt(faces) + 1)//随机产生物流快递，1-6分别对应不同的快递
                    .build();

            save(logistics);
        }

        return logistics;

    }
}
